package com.woniuxy.controller;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author 罗蔚
 * @Date 2022/4/4 13:42
 * @Description TODO
 */
@RestController
@RequestMapping("/loginController")
public class LoginController {
    @Autowired
    LoginService loginService;

    @PostMapping("/login")
    public ResponseEntity login(@RequestBody @Valid User user, HttpServletRequest request, HttpServletResponse response){
        //前段进行了非空判断, 后端进行数据的匹配
        System.out.println(user);
        User _user = loginService.login(user);
        String password = user.getPassword();
        if(_user == null || !(password.equals(_user.getPassword())) || "0".equals(_user.getStatus())){
            return ResponseEntity.FAIL;
        }else{
            request.getSession().setAttribute("user",_user.getUsername());
            return ResponseEntity.SUCCESSFUL;
        }
    }

    @GetMapping("/show")
    public ResponseEntity show(){
        return loginService.show();
    }
}
